<template>
  <div class="login-container">
    <el-row class="row">
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xs="24">
        <el-form
          class="login-form"
          :rules="rules"
          :model="loginForm"
          ref="form"
        >
          <h1>hello</h1>
          <h2>欢迎来到硅谷甄选</h2>
          <el-form-item prop="username">
            <el-input :prefix-icon="User" v-model="loginForm.username" />
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              :prefix-icon="Lock"
              type="password"
              v-model="loginForm.password"
              show-password
            />
          </el-form-item>
          <el-form-item>
            <el-button
              :loading="loading"
              style="width: 100%"
              type="primary"
              size="default"
              @click="login"
            >
              登陆
            </el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script lang="ts" setup>
import { User, Lock } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
import { reactive, ref } from 'vue'
// pinia
import useUserStore from '@/store/modules/user'
import { useRouter } from 'vue-router'

const userStore = useUserStore()
const router = useRouter()

const loginForm = reactive<FormType>({
  username: 'admin',
  password: '111111',
})

const loading = ref(false)

// 表单校验
interface FormType {
  username: string
  password: string
}
const rules = reactive({
  username: [
    {
      required: true,
      min: 5,
      max: 10,
      trigger: 'change',
      message: '账号长度至少六位',
    },
  ],
  password: [
    {
      required: true,
      min: 6,
      max: 12,
      trigger: 'change',
      message: '密码为6—12位',
    },
  ],
})

// 登陆按钮
const form = ref()
const login = async () => {
  await form.value.validate()
  loading.value = true
  try {
    await userStore.userLogin(loginForm)
    ElMessage({
      type: 'success',
      message: `${getTime()},欢迎回来`,
    })
    router.push('/')
  } catch (error) {
    ElMessage.error((error as Error).message)
  } finally {
    loading.value = false
  }
}

function getTime() {
  let message = ''
  const hours = new Date().getHours()
  if (hours <= 9) {
    message = '早上'
  } else if (hours < 12) {
    message = '上午'
  } else if (hours < 14) {
    message = '中午'
  } else if (hours <= 18) {
    message = '下午'
  } else {
    message = '晚上'
  }
  return message + `好`
}
</script>

<style scoped lang="scss">
.login-container {
  width: 100%;
  height: 100vh;
  background: url('@/assets/images/background.jpg') no-repeat;
  background-size: cover;

  .row {
    position: absolute;
    top: 30vh;
    width: 100vw;

    .login-form {
      width: 80%;
      position: relative;
      background: url('@/assets/images/login_form.png') no-repeat;
      background-size: cover;
      padding: 40px;

      h1 {
        color: #fff;
        font-size: 40px;
      }

      h2 {
        color: #fff;
        font-size: 20px;
        margin: 20px 0;
      }
    }
  }
}
</style>
